package com.anil.shapesdemo;

import java.util.List;

import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

	WebView webview;
	RequestToken requestToken;
	AccessToken accessToken;
	public String oauthToken;
	public String oauthVerifier;
	private String OAUTH_CONSUMER_KEY = "78PHaHhx3Tx2p9M4j42BQA";
	public String OAUTH_CONSUMER_SECRET = "xLQUbQHYtvcW0hYWR3TADa1qXGNx2mQ1ZnzituTbhQ";
	private ConfigurationBuilder cb;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startTwitterProcess();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public class TwitterProcess extends AsyncTask<String,Void,RequestToken> {

		@Override
		protected void onPostExecute(RequestToken result) {
			webview = (WebView)findViewById(R.id.twitterwebview);
			webview.setWebViewClient(new myWebClient());
			webview.loadUrl(result.getAuthorizationURL());
			super.onPostExecute(result);
		}

		@Override
		protected RequestToken doInBackground(String... arg0) {
			Twitter twitter = TwitterFactory.getSingleton();
		    twitter.setOAuthConsumer(OAUTH_CONSUMER_KEY,OAUTH_CONSUMER_SECRET);
		    try {
				requestToken = twitter.getOAuthRequestToken();
				System.out.println(requestToken);
			} catch (TwitterException e) {
				e.printStackTrace();
			}
             
		    return requestToken;
		}
		
	}
	public void startTwitterProcess() {
		new TwitterProcess().execute();
	}
	
	public void getHomeTimeLineforUser() {
		Twitter twitter = TwitterFactory.getSingleton();
		
		try {
			AccessToken accessToken = twitter.getOAuthAccessToken();
	        List<Status> statuses = twitter.getHomeTimeline();
	        System.out.println("Showing @"+accessToken.getScreenName()+" home timeline.");
	        for (Status status : statuses) {
	             System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
	        }
		} catch (TwitterException e) {
			e.printStackTrace();
		}
		
	}
	
	public class myWebClient extends WebViewClient
    {

		@Override
		public void onPageFinished(WebView view, String url) {
        	
		}

		@Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }
 
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
        	System.out.println(url);
        	int preStart = url.indexOf("oauth_token");
        	int preEnd = url.indexOf("&oauth_verifier");
        	oauthToken = url.substring(preStart+12, preEnd);
        	oauthVerifier = url.substring(preEnd+16);
        	System.out.println("oauth_token: "+oauthToken);
        	System.out.println("oauth_verifier: "+oauthVerifier);
        	view.stopLoading();
			view.setVisibility(View.GONE);
		    new VerifyAndStoreOAuthTokenTask().execute();
			super.onPageFinished(view, url);
            return true;

        }
    }
	
	public class VerifyAndStoreOAuthTokenTask extends AsyncTask<String,Void,Void> {

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}

		@Override
		protected Void doInBackground(String... arg0) {
			Twitter twitter = TwitterFactory.getSingleton();
			try {
				accessToken = twitter.getOAuthAccessToken(requestToken, oauthVerifier);
				System.out.println("verified accessToken : "+accessToken);
				twitter.setOAuthAccessToken(accessToken);
				getHomeTimeLineforUser();
			} catch (TwitterException e) {
				e.printStackTrace();
			} 
			return null;
		}
		
	}
		
}
